//
//  KZExperienceEventsController.m
//  xiaorizi
//
//  Created by kun on 15/10/28.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZExperienceEventsController.h"
#import "KZExperienceCell.h"
#import "UIImageView+WebCache.h"
#import "UIViewAdditions.h"
#import "UINavigationBar+Status.h"
#import "KZExperienceEventCell.h"

@interface KZExperienceEventsController ()

@property (nonatomic,assign) CGFloat cellHeight;

@property (nonatomic,assign) CGFloat cellRefreshCount;


@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic)  UIImageView *imageView;

@end

@implementation KZExperienceEventsController

- (IBAction)clickedBackButton:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


//控制头视图的出现消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [self.navigationController.navigationBar nav_clear];
    self.tableView.delegate = nil;
    self.navigationController.toolbarHidden = YES;
}

- (void)viewDidLoad {
  
    [super viewDidLoad];
    //加载头部视图
    [self createHeaderView];
    NSLog(@"---->exprienceModel %@",_model);
      [self.navigationController setToolbarHidden:NO];
    CGFloat H = self.view.height;
   // self.tableView.rowHeight =  H - 64 - 50;
    _cellHeight = H;
    //设置toolbar
    [self createToolBar];
    
    
}


- (void)createToolBar
{
    // 显示工具栏
    //self.navigationController.toolbarHidden = NO;
    
    
    //设置 toolbar的背景图片， 参数：背景图，什么位置显示()，什么模式显示（横屏，竖屏）
    //[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
    
    self.navigationController.toolbar.frame = CGRectMake(0, 0, self.view.width, 44);
    
    //通过toolbaritems来设置工具栏的显示项，数组中是UIBarButtonItem的实例
    

    
    UIButton *firstBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [firstBtn setImage:[UIImage imageNamed:@"letters_2"] forState:UIControlStateNormal];
    firstBtn.imageEdgeInsets = UIEdgeInsetsMake(0,5, 0, 15);
   // firstBtn.backgroundColor = [UIColor redColor];

    firstBtn.frame = CGRectMake(0, 0,50, 30);
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:firstBtn];
    
    [firstBtn addTarget:self action:@selector(sixinClicked:) forControlEvents:UIControlEventTouchUpInside];

    
//    [firstBtn setTitle:@"私信" forState:UIControlStateNormal];
    
   // NSDictionary * attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Heiti SC" size:14]};

    
    
    
//    NSDictionary * attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Heiti SC" size:14]};
//    [item1 setTitleTextAttributes:attributes forState:UIControlStateNormal];
    
//    
//    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"letters_2"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    UIButton *SecondBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [SecondBtn setImage:[UIImage imageNamed:@"Invite_1"] forState:UIControlStateNormal];
      SecondBtn.imageEdgeInsets = UIEdgeInsetsMake(0,5, 0, 15);
   // [SecondBtn setTitleTextAttributes:attributes forState:UIControlStateNormal];
    
    SecondBtn.frame = CGRectMake(0, 0,50, 30);
    //[SecondBtn setTitle:@"邀请TA" forState:UIControlStateNormal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithCustomView:SecondBtn];
      [SecondBtn addTarget:self action:@selector(yaoqingClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    //item 的间隔，不会显示出来,会自动计算间隔
    
    //添加报名
    UIButton *thridBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     [thridBtn setBackgroundImage:[UIImage imageNamed:@"fsyzm"] forState: UIControlStateNormal];
    [thridBtn addTarget:self action:@selector(baomingClicked:) forControlEvents:UIControlEventTouchUpInside];
    thridBtn.frame = CGRectMake(0, 0, 100, 30);
    [thridBtn setTitle:@"报名" forState:UIControlStateNormal];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithCustomView:thridBtn];
    
    [thridBtn setBackgroundColor:[UIColor orangeColor]];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//    NSArray *items = [NSArray arrayWithObjects:spaceItem, item1,spaceItem,item2,spaceItem, nil];
    
    NSArray *items = [NSArray arrayWithObjects: item1,item2,spaceItem,item3 ,nil];
    
    self.toolbarItems = items;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)baomingClicked:(UIButton *)btn
{
    NSLog(@"--->报名");
}


-(void)yaoqingClicked:(UIButton *)btn
{

NSLog(@"--->邀请");
}

- (void)sixinClicked:(UIButton *)btn
{
   NSLog(@"--->私信");
}



-(void)createHeaderView
{
#warning 5 图片放大存在问题 与真实不符合？
    _contentView.backgroundColor = [UIColor redColor];
    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0,-64,self.view.width, 200);
    [imageView sd_setImageWithURL:[NSURL URLWithString:_model.imgs]];
    _imageView = imageView;
    _imageView.contentMode = UIViewContentModeScaleToFill;
    [_contentView addSubview:imageView];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.frame = CGRectMake(0, 0, self.view.width, 134);

    self.tableView.tableHeaderView = _contentView;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KZExperienceEventCell *cell = [tableView dequeueReusableCellWithIdentifier:@"experienceEventCell" forIndexPath:indexPath];
    
    cell.model = _model;
    
    [cell setBackWebViewHeightBlock:^(KZExperienceEventCell * cell , CGFloat height) {
       //
        _cellRefreshCount ++;
    
        //防止一直刷新
        if (_cellRefreshCount == 1) {
            
            //cell的高度为webView的高度
            _cellHeight = height;
            //刷新第5个section，第1行
            NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
            NSArray *paths = [NSArray arrayWithObjects:path,nil];
            [self.tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone];
        }
        else
        {
            return;
        }
        
    }];
    
    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    self.navigationController.navigationBar.alpha = 0;
    
   // NSLog(@"1----->%@",NSStringFromCGPoint(scrollView.contentOffset));
    UIColor * color = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:249/255.0 alpha:1];
    
    
    CGFloat offSetY = scrollView.contentOffset.y;
    
    if (offSetY > -64)
    {
        CGFloat alpha =   (64+offSetY)/136;
        
        [self.navigationController.navigationBar nav_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
        
    }
    else if(offSetY < -64)
    {
        CGFloat H = -64 -offSetY;
        _imageView.frame = CGRectMake(0, -64- H, self.view.width, 200 +H);
    }
    else
    {
        [self.navigationController.navigationBar nav_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}
//


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
